package com.example.jetpacktest.activity.material_design

import android.os.Bundle
import android.widget.TableLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.ORIENTATION_VERTICAL
import com.example.jetpacktest.R
import com.example.jetpacktest.adapter.VP2Adapter001
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class ViewPager2Test001: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewpager2_test001)
        val vp2 = findViewById<ViewPager2>(R.id.view_pager_2)
        val tabLayout: TabLayout = findViewById<TabLayout>(R.id.tab_layout)

        val list = listOf<String>("a", "b", "c", "d")
        val adapter = VP2Adapter001(list, this)
        vp2.adapter = adapter
        vp2.orientation = ORIENTATION_VERTICAL //设置成竖直方向

        //必须写在下面,否则报下面这个异常
        //java.lang.IllegalStateException: TabLayoutMediator attached before ViewPager2 has an adapter
        TabLayoutMediator(tabLayout, vp2){tab, pos ->
            tab.text = "TAB-$pos"
        }.attach()
    }

}